package com.zyy.java8.samples.lambda;

import java.util.*;

/**
 * @author zhangyiying
 * @ClassName: Lambda1
 * @Description:
 * @date 2021/12/23 10:36
 */
public class Lambda1 {

    public static void main(String[] args) {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");

        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return b.compareTo(a);
            }
        });

        Collections.sort(names, (String a, String b) -> {
            return b.compareTo(a);
        });

        Collections.sort(names, (String a, String b) -> b.compareTo(a));

        Collections.sort(names, (a, b) -> b.compareTo(a));

        System.out.println(names);

        names.sort(Collections.reverseOrder());

        System.out.println(names);


        List<String> name2 = Arrays.asList("peter", null, "anna", "mike", "xenia");
        name2.sort(Comparator.nullsLast(String::compareTo));
        System.out.println(name2);

        List<String> name3 = null;

        /**
         *  Optional.of(T t); // 创建一个Optional实例
         *  Optional.empty(); // 创建一个空的Optional实例
         *  Optional.ofNullable(T t); // 若T不为null，创建一个Optional实例，否则创建一个空实例
         *  isPresent();    // 判断是够包含值,接受一个Consumer(消费者) 参数，如果对象不是空的，就对执行传入的 Lambda 表达式
         */
        Optional.ofNullable(name3).ifPresent(list -> list.sort(Comparator.naturalOrder()));

        System.out.println(name3);

    }
}
